﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XWord.WPF.ApplicationFramework;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Practices.ServiceLocation;
using XWord.GUI.Presenters.Fundamental;

namespace XWord.GUI.UserControls
{
    /// <summary>
    /// Interaction logic for SearchBar.xaml
    /// </summary>
    public partial class SearchBar : UserControl
    {
        public SearchBar()
        {
            InitializeComponent();
            
            if(System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            Element_WebEngine.ItemsSource = WordWebEngine.GetWebEngineList();
            Element_SplitButton.Tag = Element_WebEngine.Items[0];
            Element_SplitButton.Click += new EventHandler<ActiproSoftware.Windows.Controls.Ribbon.Controls.ExecuteRoutedEventArgs>(Element_SplitButton_Click);
        }

        void Element_SplitButton_Click(object sender, ActiproSoftware.Windows.Controls.Ribbon.Controls.ExecuteRoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(Element_SearchComboBox.Text)) return;
            var engine = (sender as FrameworkElement).Tag as WordWebEngine;
            var webBrowserPresenter = ServiceLocator.Current.GetInstance<WebBrowserPresenter>();
            webBrowserPresenter.DisplayName = string.Format("{0}搜索结果 by {1}", Element_SearchComboBox.Text, engine.Name);
            webBrowserPresenter.Source = new Uri(string.Format(engine.URL, Element_SearchComboBox.Text));
            webBrowserPresenter.Show();
            e.Handled = true;
        }

        private void WebEngineChanged(object sender, ActiproSoftware.Windows.Controls.Ribbon.Controls.ExecuteRoutedEventArgs e)
        {
            var engine=(sender as FrameworkElement).DataContext as WordWebEngine;
            Element_SplitButton.Tag = engine;
            if (!string.IsNullOrEmpty(Element_SearchComboBox.Text))
            {
                var webBrowserPresenter = ServiceLocator.Current.GetInstance<WebBrowserPresenter>();
                webBrowserPresenter.DisplayName =string.Format("{0}搜索结果 by {1}",Element_SearchComboBox.Text,engine.Name);
                webBrowserPresenter.Source = new Uri(string.Format(engine.URL, Element_SearchComboBox.Text));
                webBrowserPresenter.Show();
            }
            e.Handled = true;
        }

        private ICollectionView CollectionView
        {
            get { return CollectionViewSource.GetDefaultView(XWordAppConfig.Current.LatestSearchList); }
        }

        private void Element_MovePrevious_Click(object sender, RoutedEventArgs e)
        {
            if (CollectionView.CurrentPosition == 0)
            {
                CollectionView.MoveCurrentToLast();
            }
            else
            {
                CollectionView.MoveCurrentToPrevious();
            }
            
            Element_SearchComboBox.SelectedItem = CollectionView.CurrentItem;
        }

        private void Element_MoveNext_Click(object sender, RoutedEventArgs e)
        {
            if (CollectionView.CurrentPosition == (Element_SearchComboBox.Items.Count-1))
            {
                CollectionView.MoveCurrentToFirst();
            }
            else
            {
                CollectionView.MoveCurrentToNext();
            }
            Element_SearchComboBox.SelectedItem = CollectionView.CurrentItem;
        }

        private void Element_SearchComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                Element_SearchMode.SelectedIndex = (Element_SearchMode.SelectedIndex + 1) % 2;
                e.Handled = true;
            }
        }
    }
}
